<div class="judul"><?php echo "$title";?></div>
<?php
  $flashdata = $this->session->flashdata('message');
  $messages = isset ($flashdata)?pesan($flashdata):"";
  echo "$messages";
?> 
<script type="text/javascript">
    $(document).ready(function(){
        $("input[type=submit]").click(function(){
            var jumlahForm=$('.col_tr').length;
            var i=0;
            var isi=false;
            for(i=1;i<=jumlahForm;i++){
                if($('#tanggal'+i).val() != ''){
                    if($('#jumlah'+i).val() == ''){
                        alert('Jumlah pemasukan tidak boleh kosong');
                        $('#jumlah'+i).focus();
                        return false;
                    }
                    if($('#keterangan'+i).val() == ''){
                        alert('Keterangan pemasukan harus diisi');
                        $('#keterangan'+i).focus();
                        return false;
                    }
                    isi=true;
                }
            }
            if(!isi){
                alert('Inputan pemasukan masih kosong');
                return false;
            }
        })
    })
</script>
<?php
  echo form_open("$subLink");
?>
<div class="data-input">
    <fieldset>
        <legend><?php echo "$formTitle";?></legend>
        <label>Petugas</label><span style="font-size: 11px;padding-top: 7px;"><?php echo $this->session->userdata('nama');?></span>
    </fieldset>
</div>
<input type="button" value="Tambah Baris" id="tambahBaris" class="button" style="margin-bottom: 5px;" />
<br />
<div class="data-list">
    <table class="table-input" id="tbl_pemasukan" style="width: 60%">
        <tr>
            <th style="width: 5%">No</th>
            <th style="width: 15%">Tanggal</th>
            <th style="width: 15%">Jumlah (Rp.)</th>
            <th>Keterangan</th>
            <th style="width: 10%">Aksi</th>
        </tr>
        <?php
          for($i = 1;$i <= 2;$i++){
        ?>
        <tr class="col_tr <?= ($i % 2 == 0) ? 'even' : 'odd' ?>">
            <td align="center"><?php echo "$i";?></td>
            <td><?php echo form_input("tanggal[]","","id='tanggal$i' class='datepicker-input'");?></td>
            <td><?php echo form_input("jumlah[]","","class='right' id='jumlah$i' onkeyup='formatNumber(this)'");?></td>
            <td><?php echo form_input("keterangan[]","","id='keterangan$i' style='width:80%'");?></td>
            <td><input type="button" onClick="removeMe(<?php echo "$i";?>,this)" value="Hapus" /></td>
        </tr>
        <?php
          }
        ?>
    </table>
</div>    
<div class="field-group">
    <?php
      echo form_submit("add","Simpan", "class='button' style='margin-right:2px'");
      echo form_button('Batal','Batal', 'class=button onClick='."javascript:location.href='".base_url()."index.php/pemasukan/'".'');
      echo form_close();
    ?>
</div>
<script type="text/javascript">
  counters = $('.col_tr').length+1;
  $(document).ready(function(){
      $('#tambahBaris').click(function(){
          var counter = counters++,
          number = $('.col_tr').length+1;
          var string = "<tr class='col_tr'>"+
              "<td align='center'>"+number+"</td>"+
              "<td><input type='text' name='tanggal' id='tanggal"+number+"' class='datepicker-input' /></td>"+
              "<td><input type='text' name='jumlah[]' class='right' id='jumlah"+number+"' onkeyup='formatNumber(this)'/></td>"+
              "<td><input type='text' name='keterangan[]' class='keterangan' id='keterangan"+number+"' style='width:80%'></td>"+
              "<td><input type='button' onClick='removeMe("+number+",this)' value='Hapus' /></td></tr>";
          $('#tbl_pemasukan').append(string);
          if(number % 2 == 1){
                $('.col_tr:eq('+(number-1)+')').addClass('odd');
          }else if(number % 2 == 0){
                $('.col_tr:eq('+(number-1)+')').addClass('even');
          }
      })
  })
  function removeMe(count,el){
    var parent = el.parentNode.parentNode;
    parent.parentNode.removeChild(parent);
    var col=$('.col_tr');
    var countCollectTr=col.length;
    for(var i=0;i<countCollectTr;i++){
        $('.col_tr:eq('+i+')').children('td:eq(0)').html(i+1);
        $('.col_tr:eq('+i+')').removeClass('even');
        $('.col_tr:eq('+i+')').removeClass('odd');
        if((i+1) % 2 == 1){
            $('.col_tr:eq('+i+')').addClass('even');
        }else{
            $('.col_tr:eq('+i+')').addClass('odd');
        }
        $('.col_tr:eq('+i+')').children('td:eq(1)').children('.datepicker-input').attr('id', 'tanggal'+(i+1));
        $('.col_tr:eq('+i+')').children('td:eq(2)').children('.right').attr('id', 'jumlah'+(i+1));
        $('.col_tr:eq('+i+')').children('td:eq(3)').children('.keterangan').attr('id', 'keterangan'+(i+1));
  }}
</script>